<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';
import { CustomMouseMenu } from '@/plugins/directive/contextmenu';
import { ElMessageBox, ElMessage } from 'element-plus';
import useStore from '@/store';
import { LicenseEnum } from '@/enums/licence';
import { props } from './organization';
import { WindowType } from '@/types/global/window';
defineOptions({ name: 'Organization' });
defineProps(props);
const tableRef = ref(null);
const loading = ref(true);
const currentRow = ref(null);
const organizations = ref(null);
const handleTableRowClick = row => {
  currentRow.value = row;
};
const remove = () => {
  if (!currentRow.value) {
    ElMessage.error('请选择要删除的组织机构!');
    return;
  }
  let confirmMessage = '是否删除选中的模块?';
  // if (ids.constructor === Number) {
  //   confirmMessage = '是否删除模块（' + name + '）?';
  //   ids = [ids];
  // }

  ElMessageBox.confirm(confirmMessage, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      // window.$api.module.remove(ids).then(res => {
      //   ElMessage.success('删除成功');
      //   query();
      // });
    })
    .catch(() => {});
};
const query = (): void => {
  window.$api.organization.query().then(res => {
    organizations.value = res.data;
    loading.value = false;
  });
};
const add = () => {
  const window: WindowType = { name: 'OrganizationForm', component: 'OrganizationForm', label: '组织机构添加', icon: 'edit', width: 600, height: 500 };
  useStore.window.windowAdd(window);
};
const edit = () => {
  if (!currentRow.value) {
    ElMessage.error('请选择要修改的组织机构!');
    return;
  }
  const window: WindowType = {
    name: 'OrganizationForm' + currentRow.value.id,
    component: 'OrganizationForm',
    label: '组织机构修改（' + currentRow.value.name + '）',
    icon: 'edit',
    width: 600,
    height: 500,
    params: { id: currentRow.value.id },
  };
  useStore.window.windowAdd(window);
};

const menus = reactive([
  {
    label: '修改',
    icon: 'Edit',
    fn: () => {
      edit();
    },
    license: LicenseEnum.SYS_ORGANIZATION_MODIFY,
  },
  {
    label: '删除',
    icon: 'Delete',
    fn: () => {
      remove();
    },
    license: LicenseEnum.SYS_ORGANIZATION_REMOVE,
  },
]);
const showMouseMenu = (row, column, event) => {
  const ctx: any = CustomMouseMenu({
    el: event.currentTarget,
    mouse: event,
    ...{ menus },
  });
  ctx.show();
  currentRow.value = row;
  tableRef.value.setCurrentRow(row);
};
onMounted(() => {
  query();
});
</script>

<template>
  <windows-window :window="window">
    <template #button>
      <windows-button-group size="18">
        <windows-button icon="CirclePlus" :license="LicenseEnum.SYS_ORGANIZATION_ADD" @click="add">新建</windows-button>
        <windows-button icon="Edit" :license="LicenseEnum.SYS_ORGANIZATION_MODIFY" split @click="edit"></windows-button>
        <windows-button icon="Delete" :license="LicenseEnum.SYS_ORGANIZATION_REMOVE"></windows-button>
      </windows-button-group>
    </template>
    <el-table
      ref="tableRef"
      :data="organizations"
      style="width: 100%"
      height="100%"
      row-key="id"
      :tree-props="{ hasChildren: 'hasChildren' }"
      highlight-current-row
      default-expand-all
      @row-contextmenu="showMouseMenu"
      v-loading="loading"
      @row-click="handleTableRowClick"
    >
      <el-table-column prop="name" label="组织机构名称" align="left" minWidth="200">
        <template #default="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="type" label="类型" align="center">
        <template #default="scope">
          <el-tag effect="dark">{{ useStore.dictionary.getComboBoxItemName('organizationtype', scope.row.type) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="telephone" label="电话号码" align="center" width="200"></el-table-column>
      <el-table-column prop="fax" label="传真号码" align="center" width="200"></el-table-column>
      <el-table-column prop="email" label="邮箱" align="center" width="200"></el-table-column>
      <el-table-column prop="sort" label="排序" align="center" width="100"></el-table-column>
      <el-table-column prop="remark" label="备注" align="center" minWidth="200"></el-table-column>
      <!-- <el-table-column prop="createdOn" label="创建日期" width="150"></el-table-column>
      <el-table-column prop="createdByName" label="创建人" width="100"></el-table-column>
      <el-table-column prop="updatedOn" label="更新日期" width="150"></el-table-column>
      <el-table-column prop="updatedByName" label="更新人" width="100"></el-table-column> -->
    </el-table>
  </windows-window>
</template>

<style scoped lang="scss">
#organization .el-table .cell {
  display: flex;
  align-items: center;
}
.cell_name {
  display: flex;
  align-items: center;
  span {
    margin-left: 5px;
  }
}
.btngroup {
  height: 50px;
  display: flex;
  align-items: center;
  .el-icon {
    font-size: 18px;
    color: #909399;
    margin-right: 15px;
  }
}
</style>
